<?php
class STE_News extends STE_Db
{
	public function getNewsList($languageID, $categoryID, $from, $numberOfNews)
	{
		try
		{
			$query = sprintf("CALL sp_admin_get_news_list(%u, %u, %u, %u)", $languageID, $categoryID, $from, $numberOfNews);
			$news = $this->_db->fetchAll($query);
			$this->_db->closeConnection();
			return $news;
		}
		catch (Exception $ex)
		{
			throw $ex;
		}
	}
	
	public function addNews($categoryID, $languageID, $title, $isHighlight, $isHot, $publishDate, $source, $imageURL, $summary, $content, $createdBy)
	{
		try
		{
			$newsInfo = array();
			$newsInfo["categoryID"] = $categoryID;
			$newsInfo["isHighlight"] = $isHighlight;
			$newsInfo["isHot"] = $isHot;
			$newsInfo["source"] = $source;
			$newsInfo["publishDate"] = $publishDate;
			$newsInfo["createdBy"] = $createdBy;
			$newsInfo["createdDate"] = date("Y-m-d H:i:s");
			$this->_db->insert("news", $newsInfo);
			$newsID = $this->_db->lastInsertId("news");
			
			$newsLanguageInfo = array();
			$newsLanguageInfo["newsID"] = $newsID;
			$newsLanguageInfo["languageID"] = $languageID;
			$newsLanguageInfo["title"] = $title;
			$newsLanguageInfo["content"] = $content;
			$newsLanguageInfo["image_path"] = $imageURL;
			$newsLanguageInfo["sub_content"] = $summary;
			$newsLanguageInfo["createdBy"] = $createdBy;
			$newsLanguageInfo["createdDate"] = date("Y-m-d H:i:s");
			$this->_db->insert("news_language", $newsLanguageInfo);
		}
		catch (Exception $ex)
		{
			throw $ex;
		}
	}
	
	public function editNews($newsID, $categoryID, $languageID, $title, $isHighlight, $isHot, $publishDate, $source, $imageURL, $summary, $content, $updatedBy)
	{
		try
		{
			$newsInfo = array();
			$newsInfo["categoryID"] = $categoryID;
			$newsInfo["isHighlight"] = $isHighlight;
			$newsInfo["isHot"] = $isHot;
			$newsInfo["source"] = $source;
			$newsInfo["publishDate"] = $publishDate;
			$newsInfo["updatedBy"] = $updatedBy;
			$newsInfo["updatedDate"] = date("Y-m-d H:i:s");
			$this->_db->update("news", $newsInfo, "newsID=" . $newsID);
			
			$newsLanguageInfo = array();
			$newsLanguageInfo["languageID"] = $languageID;
			$newsLanguageInfo["title"] = $title;
			$newsLanguageInfo["content"] = $content;
			$newsLanguageInfo["image_path"] = $imageURL;
			$newsLanguageInfo["sub_content"] = $summary;
			$newsLanguageInfo["updatedBy"] = $updatedBy;
			$newsLanguageInfo["updatedDate"] = date("Y-m-d H:i:s");
			$this->_db->update("news_language", $newsLanguageInfo, "newsID=" . $newsID);
		}
		catch (Exception $ex)
		{
			throw $ex;
		}
	}
	
	public function getNews($newsID)
	{
		try
		{
			$query = sprintf("CALL sp_admin_get_news(%u)", $newsID);
			$news = $this->_db->fetchRow($query);
			$this->_db->closeConnection();
			return $news;
		}
		catch (Exception $ex)
		{
			throw $ex;
		}
	}
}	
?>